<?php
/**
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                   O\  =  /O
 *                ____/`---'\____
 *              .'  \\|     |//  `.
 *             /  \\|||  :  |||//  \
 *            /  _||||| -:- |||||-  \
 *            |   | \\\  -  /// |   |
 *            | \_|  ''\---/''  |   |
 *            \  .-\__  `-`  ___/-. /
 *          ___`. .'  /--.--\  `. . __
 *       ."" '<  `.___\_<|>_/___.'  >'"".
 *      | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *      \  \ `-.   \_ __\ /__ _/   .-` /  /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
 *             佛祖保佑       永无BUG   如期完成
 */

use Phalcon\DI\FactoryDefault;
use Phalcon\Mvc\Application;

ini_set('display_errors', '1');
error_reporting(E_ALL);

date_default_timezone_set('Asia/Shanghai');

/**
 * Define some useful constants
 */
define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');

function php_error_handler($errno, $errstr, $errfile, $errline)
{
    global $application,$di;
    //查看错误级别
    if (in_array($errno, [E_NOTICE, E_WARNING])) {
        $message = $errfile . $errline . $errstr;
        throw new \Exception($message);
    }
    $message = $errstr . '=>' . $errfile. '[' . $errline. ']';
    if ($di->getConfig()->environment=='dev') {
        exit($message);
    }
    $application->logger->error($message);
    $application->view->setVar('message', 'Catch error:Your Request Page is not exist');
    $content = $application->view->render('public', 'error');
    $application->response->setContent($content);
    $application->response->setStatusCode(400);
    $application->response->send();
}

function exception_handler($e)
{
    global $di;
    global $application;
    if ($di->getConfig()->environment=='dev') {
        throw  $e;
    }
    $message ='Catch Exception:' . $e->getMessage() . ':' . $e->getTraceAsString();
    $application->logger->error($message);
    $application->view->setVar('message', 'Catch exception:Your Request Page is not exist');
    $content = $application->view->render('public', 'error');
    $application->response->setContent($content);
    $application->response->setStatusCode(400);
    $application->response->send();
}

    $di = new FactoryDefault();

    include APP_PATH . "/config/services.php";

    $config = $di->getConfig();
    include APP_PATH . '/config/loader.php';

    $application = new Application($di);
if ($config->environment == 'dev') {
    require '../libs/Debug.php';
    $application->logger->log('Request Start');
    $debug = new Debug();
    $debug->start();
}
    //设置错误处理
    set_error_handler('php_error_handler');
    //异常处理
    set_exception_handler('exception_handler');
    //关闭自动渲染
    $application->useImplicitView(false);

    $response = $application->handle();
    $response->send();
if ($config->environment == 'dev') {
    $debug->end();
    $debug->calculation();
    $messgae = 'use time:' . $debug->useTime . "sec\n";
    $messgae .= 'use memory:' . $debug->useMemory.'kb';
    $application->logger->info($messgae);
    $application->logger->log('Request End');
}
